package com.sczmgk.frame.http;

import android.content.Context;
import android.content.SharedPreferences;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class DownloadFileSaveUtil {
	
	private static final String TAG ="DownloadFileSaveUtil";
	
	public static final String NAME_DOWNLOAD = "name_download";
	
	public static final String VERSION_CODE_XML = "versionCode";
	
	public static final int VERSION_CODE_XINXIN = -1;
	public static final int VERSION_CODE_P3A = 2;
	public static int getVersionCode(Context ctx){
		SharedPreferences spf = ctx.getSharedPreferences(NAME_DOWNLOAD, Context.MODE_WORLD_WRITEABLE);
		int versionCode = spf.getInt(VERSION_CODE_XML, VERSION_CODE_XINXIN);
		return versionCode;
	}
	
	public static boolean refreshVersionCode(Context ctx){
		boolean status = false;
		SharedPreferences spf = ctx.getSharedPreferences(NAME_DOWNLOAD, Context.MODE_WORLD_WRITEABLE);
		SharedPreferences.Editor editor = spf.edit();
		if (spf!=null) {
			editor.putInt(VERSION_CODE_XML, VERSION_CODE_P3A);
			status = editor.commit();
		}
		return status;
	}
	
	public static boolean saveFile(Context ctx, String url, DownloadFileSave fileSave) {
		boolean status = false;
		SharedPreferences spf = ctx.getSharedPreferences(NAME_DOWNLOAD, Context.MODE_WORLD_WRITEABLE);
		SharedPreferences.Editor editor = spf.edit();
		if (spf!=null) {
			editor.putString(url, fileSave.getGather());
			status = editor.commit();
		}
		return status;
	}
	public static DownloadFileSave getFile(Context ctx, String url) {
		SharedPreferences spf = ctx.getSharedPreferences(NAME_DOWNLOAD, Context.MODE_WORLD_WRITEABLE);
		String gather = spf.getString(url, null);
		if(gather!=null){
			DownloadFileSave downloadFile = DownloadFileSave.parser(gather,url);
			if(downloadFile!=null&&downloadFile.getDownloaStatus() != 
					DownloadFileSave.DOWNLOAD_STATUS_BAD){
				return downloadFile;
			}else{
				deleteFile(ctx, url);
			}
		}
		return null;
	}
	
	public static boolean deleteCacheXml(Context context){
		boolean status = false;
		SharedPreferences spf = context.getSharedPreferences(NAME_DOWNLOAD, Context.MODE_WORLD_WRITEABLE);
		SharedPreferences.Editor editor = spf.edit();
		if (spf!=null) {
			editor.clear();
			status = editor.commit();
		}
		return status;
	}
	public static String getUsableCachePath(Context ctx, String cacheUrl){
		DownloadFileSave downloadFile = getFile(ctx, cacheUrl);
		if(downloadFile!=null&&downloadFile.getDownloaStatus()== DownloadFileSave.DOWNLOAD_STATUS_FINISH){
			return downloadFile.getFilePath();
		}
		return null;
	}
	
	public static boolean deleteFile(Context ctx, String key) {
		SharedPreferences spf = ctx.getSharedPreferences(NAME_DOWNLOAD, Context.MODE_WORLD_WRITEABLE);
		SharedPreferences.Editor editor = spf.edit();
		editor.remove(key);
		return editor.commit();
	}
	
	private static List<DownloadFileSave> getAllDownloadFile(Context context){
		List<DownloadFileSave> downloadFileList = new ArrayList<DownloadFileSave>();
		try{
			SharedPreferences spf = context.getSharedPreferences(NAME_DOWNLOAD, Context.MODE_WORLD_WRITEABLE);
			Map<String, String> mapArraryMap = (Map<String, String>) spf.getAll();
			Iterator<String> iterator = mapArraryMap.keySet().iterator();
			while(iterator.hasNext()){
				String urlKey =  iterator.next();
				String downloadContent = mapArraryMap.get(urlKey);
				DownloadFileSave downloadFile = DownloadFileSave.parser(downloadContent,urlKey);
				if(downloadFile!=null){
					downloadFileList.add(downloadFile);
				}
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return downloadFileList;
	}
	
	public static class DownloadFileSave{
		public static final long FIXED_LENGTH = 10086;
		
		public static final int DOWNLOAD_STATUS_FINISH = 100;
		
		public static final int DOWNLOAD_STATUS_BAD = 200;
		
		public static final int DOWNLOAD_STATUS_DOWNLOADING = 300;
		
		public String mUrl;
		public String mFilePath;
		public final long mNetworkFileLength;
		public final long mFileLength;
		public final long mLastReadTime;
		public boolean isUpload = false;
		
		public static final String COMPART = ",luochun";
		public DownloadFileSave(String filePath){
			mFilePath = filePath;
			mNetworkFileLength = FileUtil.getFileLength(filePath);
			mFileLength = mNetworkFileLength;
			mLastReadTime = System.currentTimeMillis();
		}
		
		public DownloadFileSave(String filePath, boolean isUpload){
			mFilePath = filePath;
			mNetworkFileLength = FileUtil.getFileLength(filePath);
			mFileLength = mNetworkFileLength;
			mLastReadTime = System.currentTimeMillis();
			this.isUpload = isUpload;
		}
		public DownloadFileSave(String filePath, long networkFileLength){
			mFilePath = filePath;
			mNetworkFileLength = networkFileLength;
			mFileLength = FileUtil.getFileLength(filePath);
			mLastReadTime = System.currentTimeMillis();
		}
		
		public DownloadFileSave(String filePath, long networkFileLength, long lastReadTime, String url, boolean isUpload){
			mFilePath = filePath;
			mNetworkFileLength = networkFileLength;
			mFileLength = FileUtil.getFileLength(filePath);
			mLastReadTime = lastReadTime;
			mUrl = url;
		}
		
		public String getFilePath(){
			return mFilePath;
		}
		
		private static DownloadFileSave parser(String gather, String urlKey){
			try{
				if(gather.contains(COMPART)){
					String[] gatherArrary = gather.split(COMPART);
					try{
						if(gatherArrary.length==2){
							String filePath = gatherArrary[0];
							long networkFileLength = Long.parseLong(gatherArrary[1]);
							return new DownloadFileSave(filePath, networkFileLength);
						}
						else if(gatherArrary.length==4){
							String filePath = gatherArrary[0];
							long networkFileLength = Long.parseLong(gatherArrary[1]);
							long lastReadTime = Long.parseLong(gatherArrary[2]);
							boolean isUpload = Boolean.parseBoolean(gatherArrary[3]);
							return new DownloadFileSave(filePath, networkFileLength,lastReadTime,urlKey,isUpload);
						}
						else{
							return null;
						}
					}catch(Exception ex){
						return null;
					}
				}
				else{
					String filePath = gather;
					return new DownloadFileSave(filePath);
				}
			}catch(Exception ex){
				ex.printStackTrace();
				return null;
			}
		}
		
		private String getGather(){
			return mFilePath+COMPART+mNetworkFileLength+COMPART+mLastReadTime+COMPART+isUpload;
		}
		
		
		public int getDownloaStatus(){
			if(mFilePath==null||!new File(mFilePath).isFile()||mFileLength>mNetworkFileLength){
				return DOWNLOAD_STATUS_BAD;
			}
			else if(mNetworkFileLength == mFileLength){
				return DOWNLOAD_STATUS_FINISH;
			}
			else {
				return DOWNLOAD_STATUS_DOWNLOADING;
			}
		}
		
	}
}
